package com.spider.site.web.task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.spider.site.service.SpiderService;
import com.spider.site.web.processor.SpiderAQYProcessor;
import com.spider.site.web.processor.SpiderDYProcessor;
import com.spider.site.web.processor.SpiderHuyaProcessor;
import com.spider.site.web.processor.SpiderLZProcessor;
import com.spider.site.web.processor.SpiderRRProcessor;
import com.spider.site.web.processor.SpiderZQProcessor;
import com.spider.site.web.processor.SpiderZYProcessor;

import us.codecraft.webmagic.Spider;

/**
 * 调度
 * 
 * @author wq
 *
 */
@Component
public class SpiderTask {

	@Autowired
	private SpiderService spiderService;

	@Autowired
	private SpiderDYProcessor spiderDYProcessor;// 斗鱼定制

	@Autowired
	private SpiderHuyaProcessor spiderHuyaProcessor;// 虎牙定制

	@Autowired
	private SpiderZQProcessor spiderZQProcessor;// 战旗定制

	@Autowired
	private SpiderZYProcessor spiderZYProcessor;//章鱼 定制
	
	@Autowired
	private SpiderLZProcessor spiderLZProcessor;//龙珠定制
	
	@Autowired
	private SpiderAQYProcessor spiderAQYProcessor;//爱奇艺定制
	
	@Autowired
	private SpiderRRProcessor spiderRRProcessor;//人人
	
	@Value("${selenuim.config}")
	private String configPath;

	@Scheduled(fixedDelay = 60 * 1000)
	public void task() {
		System.out.println("调度任务开始执行 ----调度时间 时隔 60 秒");
		this.spiderService.getAll().parallelStream().forEach(result -> {// 循环
			if (result.getIsSpin() == 1 && result.getIsSpinging() == 0) {
				switch (result.getSpiderPlatform()) {
				case "斗鱼":
					Spider.create(this.spiderDYProcessor).addUrl(result.getUrl()).thread(3).runAsync();// 创建爬虫，开始爬取
					break;
				case "虎牙":
					Spider.create(this.spiderHuyaProcessor).addUrl(result.getUrl()).thread(3).runAsync();// 创建爬虫，开始爬取
					break;
				case "战旗":
					Spider.create(this.spiderZQProcessor).addUrl(result.getUrl()).thread(3).runAsync();// 创建爬虫，开始爬取
					break;
				case "乐视（章鱼）":
					Spider.create(this.spiderZYProcessor).addUrl(result.getUrl()).thread(3).runAsync();// 创建爬虫，开始爬取
					break;
				case "PP（龙珠）":
					Spider.create(this.spiderLZProcessor).addUrl(result.getUrl()).thread(3).runAsync();// 创建爬虫，开始爬取
					break;
				case "爱奇艺":
					Spider.create(this.spiderAQYProcessor).addUrl(result.getUrl()).thread(3).runAsync();// 创建爬虫，开始爬取
					break;
				case "人人":
					Spider.create(this.spiderRRProcessor).addUrl(result.getUrl()).thread(3).runAsync();// 创建爬虫，开始爬取
					break;
				default:
					System.out.println("系统繁忙");
					break;
				}
			}
		});
	}

}
